﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoreEx.Common.Proxy;
using LinFu.IoC.Configuration;
using LinFu.Proxy.Interfaces;
using Mono.Cecil;
using CoreEx.Common.Extensions;

namespace CoreEx.Common.Implementation.Proxy
{
    /// <summary>
    /// Modifies an <see cref="IProxy"/> type to include a static <see cref="IConstructorAroundInvoke"/> property.
    /// </summary>
    [Implements(typeof(ITypeModifier), ServiceName = "ConstructorInterceptorModifier")]
    public class ConstructorInterceptorModifier : ITypeModifier
    {
        public void BeforeConstruction(Type originalBaseType, IEnumerable<Type> baseInterfaces, ModuleDefinition module, TypeDefinition targetType)
        {
            
        }

        public void AfterConstruction(Type originalBaseType, IEnumerable<Type> baseInterfaces, ModuleDefinition module, TypeDefinition targetType)
        {                        
            var constructorAroundInvokeType = module.Import(typeof(IConstructorInterceptor));
            targetType.AddStaticProperty("ConstructorInterceptor", constructorAroundInvokeType);        
        }
    }
}
